package com.JavaWk;

import java.util.Scanner;

public class BookTest {
    static int lenth;
    public static void sortByPrice(Book[] b, int l, int r){
        if(l >= r){
            return;
        }
        int i = l - 1, j = r + 1, mid = l + r >> 1;
        double x = b[mid].getBook_price();
        while(i < j){
            do{
                i ++ ;
            }
            while(b[i].getBook_price() < x);
            do{
                j -- ;
            }
            while(b[j].getBook_price() > x);

            if(i < j){
                Book tmp = b[i];
                b[i] = b[j];
                b[j] = tmp;
            }
        }
        sortByPrice(b, l, j);
        sortByPrice(b, j + 1, r);
    }

    public static String searchByAuthor(Book[] b, String author){
        for(int i = 0; i < lenth; i ++ ){
            if(b[i].getBook_Author().equals(author)){
                return b[i].to_String();
            }
        }
        return "error!";
    }
    public static void main(String[] args) {
        final int MaxSize = 100000;
        Scanner in = new Scanner(System.in);
        Book[] books = new Book[MaxSize];
        int id = 0;
        boolean flag = true, flag2 = true;
        while(flag){
            books[id] = new Book();
            System.out.println("请输入书名,作者，价格");
            String n, a;
            double p;
            int option;
            n = in.next();
            a = in.next();
            p = in.nextDouble();
            books[id].setBook_Author(a);
            books[id].setBook_Name(n);
            books[id ++ ].setBook_price(p);
            System.out.println("是否继续输入？ 1/0");
            option = in.nextInt();
            lenth ++ ;
            if(option == 0){
                flag = false;
            }
        }

        sortByPrice(books, 0 , lenth - 1);

//        showInfo(books);
        for(int i = 0; i < lenth; i ++ ){
            System.out.println(books[i].to_String());
        }

        while(flag2){
            String n;
            int op;
            System.out.println("输入作者名：");
            n = in.next();
            System.out.println(searchByAuthor(books, n));
            System.out.println("是否继续查找？ 1 / 0");
            op = in.nextInt();
            if(op == 0){
                flag2 = false;
            }
        }
    }
}